GroupTalents = LibStub("AceAddon-3.0"):NewAddon("GroupTalents","AceEvent-3.0","AceConsole-3.0")
local self = GroupTalents
local addon_name = self:GetName()
local send_msg_header = addon_name .. ":" .. "当前团队结构(小队1-小队5)"
local send_msg_footer = "---"

local TalentQuery = LibStub:GetLibrary("LibTalentQuery-1.0")

local LGT = LibStub:GetLibrary("LibGroupTalents-1.0");


local tank_num = 0
local tank_roster = ""
local melee_num = 0
local melee_roster = ""
local caster_num = 0
local caster_roster = ""
local healer_num = 0
local healer_roster = ""
local unknown_num = 0
local unknown_roster = ""

function self:OnInitialize()

end

function self:OnEnable()
	self:RegisterChatCommand("gt", "SlashFunc")
	
	LGT.RegisterCallback(self, "LibGroupTalents_RoleChange")
	LGT.RegisterCallback(self, "LibGroupTalents_Update")
	LGT.RegisterCallback(self, "LibGroupTalents_UpdateComplete")
	LGT.RegisterCallback(self, "LibGroupTalents_Add")
	LGT.RegisterCallback(self, "LibGroupTalents_Remove")
	
	
	self:RegisterEvent("RAID_ROSTER_UPDATE")
	self:RegisterEvent("PLAYER_LOGON")
	
end




function self:SlashFunc()
	self:PrintRaidTalents()
end

function self:PrintRaidTalents()

	SendChatMessage(send_msg_header,"RAID",nil,nil);
	SendChatMessage("坦克(" .. tostring(tank_num) .. "):" .. tank_roster ,"RAID",nil,nil);

	SendChatMessage("菜刀(".. tostring(melee_num) .. "):" .. melee_roster ,"RAID",nil,nil);

	SendChatMessage("远程(".. tostring(caster_num) .. "):" .. caster_roster ,"RAID",nil,nil);

	SendChatMessage("大奶(".. tostring(healer_num) .. "):" .. healer_roster ,"RAID",nil,nil);

	SendChatMessage("未知(".. tostring(unknown_num) .. "):".. unknown_roster ,"RAID",nil,nil);

	SendChatMessage(send_msg_footer,"RAID",nil,nil);

end

function self:LibGroupTalents_RoleChange(guid, unit, newrole, oldrole)
	self:Calculate()
end

function self:LibGroupTalents_Update()
	self:Calculate()
end

function self:LibGroupTalents_UpdateComplete()
	self:Calculate()
end

function self:LibGroupTalents_Add(guid, unit, name, realm)
	self:Calculate()
end

function self:LibGroupTalents_Remove(guid, name, realm)
	self:Calculate()
end

function self:PLAYER_LOGON()
	self:Calculate()
end

function self:RAID_ROSTER_UPDATE()
	self:Calculate()
end

function self:Calculate()
	tank_num = 0
	tank_roster = ""
	melee_num = 0
	melee_roster = ""
	caster_num = 0
	caster_roster = ""
	healer_num = 0
	healer_roster = ""
	unknown_num = 0
	unknown_roster = ""

	for index = 1 , GetNumRaidMembers() do
		local name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML = GetRaidRosterInfo(index)
		self:Analyze(name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML, index)
	end
	
end

function self:Analyze(name, rank, subgroup, level, class, fileName, zone, online, isDead, role, isML, index)
	

	if ( subgroup > 5 ) then return end
	
	if not name then return end
	if not online then name = name .."(电梯中)" end
	
	local unitid = "raid"..tostring(index)
	
	
	local unitrole = LGT:GetUnitRole(unitid)
	
	
	if unitrole then 
		if unitrole == "tank" then
			tank_num = tank_num + 1
			tank_roster = tank_roster .. name .. ","
		elseif unitrole == "melee" then
			melee_num = melee_num + 1
			melee_roster = melee_roster .. name .. ","
		elseif unitrole == "caster" then
			caster_num = caster_num + 1
			caster_roster = caster_roster .. name .. ","
		elseif unitrole == "healer" then
			healer_num = healer_num + 1
			healer_roster = healer_roster .. name .. ","
		end
	else
		unknown_num = unknown_num + 1
		unknown_roster = unknown_roster .. name .. ","
	end

end


